﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TimViec
{
    public partial class ViecLamGoiY : System.Web.UI.Page
    {
        public int Trang { set; get; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["page"] != null)
            {
                Trang = int.Parse(Request.QueryString["page"]);
            }

            if(!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            if (Session["NguoiDung"] != null)
            {
                DAL.User user = BLL.NguoiDung.NguoiDung.Get_UserEmail(Session["NguoiDung"].ToString());
                if(user!=null)
                {
                    if (user.UrlAvatar == "")
                        avartar.ImageUrl = "images/user.png";
                    else
                    {
                        avartar.ImageUrl = user.UrlAvatar;

                    }

                    lbUserName.Text = user.UserName;
                }

                ListItem item = new ListItem();
                item.Text = "Tất cả ngành nghế";
                item.Value = "-1";
                item.Selected = true;

                ListItem item1 = new ListItem();
                item1.Text = "Tất cả nơi làm việc";
                item1.Value = "-1";
                item1.Selected = true;

                dropNganhNghe.DataSource = BLL.Admin.NganhNgheHelper.List_NganhNghe_ValueConfig(0);
                dropNganhNghe.DataTextField = "Ten";
                dropNganhNghe.DataValueField = "NganhNgheId";
                dropNganhNghe.DataBind();
                dropNganhNghe.Items.Add(item);

                dropNoiLamViec.DataSource = BLL.Admin.TinhHelper.List_ALL_Tinh();
                dropNoiLamViec.DataTextField = "Ten";
                dropNoiLamViec.DataValueField = "TinhId";
                dropNoiLamViec.DataBind();
                dropNoiLamViec.Items.Add(item1);

                string path = HttpContext.Current.Request.Url.AbsolutePath;

                int page = 1;

                if (Request.QueryString["page"] != null)
                {
                    page = int.Parse(Request.QueryString["page"]);
                }

                var data = BLL.NguoiDung.TuyenDung.GoiYViecLam();
                rptGoiY.DataSource = data.Skip((page - 1)*10).Take(10);
                rptGoiY.DataBind();

                int Count = data.Count();
                int TotalPage = Count/10 + 1;
                if (Count < 1)
                {
                    lbtPreview.Visible = false;
                    lbtNext.Visible = false;
                }
                if (page == 1)
                {
                    lbtPreview.Visible = false;
                }
                if (page == TotalPage)
                {
                    lbtNext.Visible = false;
                }

                for (int i = page - 4; i < page + 4; i++)
                {
                    if (i < 1)
                        continue;
                    if (i > TotalPage)
                        continue;
                    if (i == page && Count > 0)
                        ltlPaging.Text += "<li><a class='current' href='" + path + "?&page=" + i + "'>" + i +
                                          "</a></li>";
                    else
                        ltlPaging.Text += "<li><a href='" + path + "?&page=" + i + "'>" + i + "</a></li>";
                }
            }
            else
            {
                
                Response.Redirect("/");
            }
        }

        protected void lbtPreview_Click(object sender, EventArgs e)
        {
            string path = HttpContext.Current.Request.Url.AbsolutePath;
        }

        protected void lbtNext_Click(object sender, EventArgs e)
        {
            string path = HttpContext.Current.Request.Url.AbsolutePath;
        }

        protected void btSearch_Click(object sender, EventArgs e)
        {
            var str = "";
            if (txtSearch.Text != "")
            {
                str = "&tk=" + txtSearch.Text;
            }
            Response.Redirect("/tim-viec-lam.html?nganhnghe=" + dropNganhNghe.SelectedValue + "&noilam=" + dropNoiLamViec.SelectedValue + "&lh=" + dropLoaiHinhCongViec.SelectedValue + str);
        }
    }
}